package main.pmc;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.SmallTool;
import utils.StringUtil;
import utils.httpclient.HttpClient;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: gcr
 * @Date: 2022/3/12 13:20
 */
public class DownPmcidHtmlParseTestTTT {
    // 用于测试页面获取文件使用

    @SneakyThrows
    public static void main(String[] args) {

        String html = "";
//        String pmc = "https://pmc.ncbi.nlm.nih.gov/articles/PMC11724815";
//        String pmc = "https://pmc.ncbi.nlm.nih.gov/articles/PMC8817215"; // 1
        String pmc = "https://pmc.ncbi.nlm.nih.gov/articles/PMC8772656";
        try {
            html = HttpClient.getHtmlDirect(pmc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashSet<String> urlSet = new HashSet<>();

        Document parse = Jsoup.parse(html);
        Element element1 = parse.selectFirst("section.associated-data");
        if (element1 != null) {
            System.out.println("++ section.associated-data");
            Elements a = element1.select("a");
            for (Element element : a) {
//                            System.out.println(element.attr("href"));
                urlSet.add(element.attr("href"));
            }
        }
        System.out.println("associated size + " + urlSet.size());
        Elements element2 = parse.select("h2:contains(Supplementary Material)");
        if (element2 != null) {
            System.out.println("++h2:contains(Supplementary Material)");
            if (element2.size() > 1) {
                System.out.println("大于1Supplementary Material");
            }
            for (Element elementParam : element2) {
                Element parent = elementParam.parent();
                Elements a = parent.select("a");
                for (Element element : a) {
//                            System.out.println(element.attr("href"));
                    urlSet.add(element.attr("href"));
                }
            }

        }
        // Appendix. Supplementary materials
        Element element3 = parse.selectFirst("h2:contains(Appendix. Supplementary materials)");
        if (element3 != null) {
            System.out.println("++h2:contains(Appendix. Supplementary materials)");
            Element parent = element3.parent();
            Elements a = parent.select("a");
            for (Element element : a) {
                urlSet.add(element.attr("href"));
            }
        }
        // Abbreviations
        Element element4 = parse.selectFirst("div:contains(Multimedia Appendix)");
        if (element4 != null) {
            System.out.println("++div:contains(Multimedia Appendix)");
            Element parent = element4.parent();
            Elements a = parent.select("a");
            for (Element element : a) {
                urlSet.add(element.attr("href"));
            }
        }


        List<String> ls = new ArrayList<>();
        if (!urlSet.isEmpty()) {
            for (String s : urlSet) {
                if (!s.startsWith("http")) {
                    ls.add("https://pmc.ncbi.nlm.nih.gov/" + s);
                } else {
                    ls.add(s);
                }

            }
        }
        System.out.println(urlSet);

    }


}
